import Taro from '@tarojs/taro'

const getToken = ()=>{
    return Taro.getStorageSync("token") || ""
}

// 统一时间格式转换 YYYY-MM-DD hh:mm:ss
/**
 * param:  "day" | "time" 保留到年|月|日，或者 时|分|秒(默认)
 */
function unioformDateTransform(date, param = "time"){
    let strDate = new Date(date).toLocaleString()
    let days = strDate.split(" ")[0]
    let time = strDate.split(" ")[1]
    let year = days.split("/")[0]
    let month = days.split("/")[1]
    let day = days.split("/")[2]

    if(Number(month)<10){
        month = '0'+ month
    }
    if(Number(day)<10){
        day = '0'+day
    }
    days =  year+'-'+month+'-'+day

    switch(param){
        case "day":
            return days
        case "time":
            return days + " " + time
    }
}

// 随机token后续要加个时间唯一标识,防止重复
function randomToken() {
    let date = new Date().toLocaleString().split(/[\/,:,\ ]/).join("");
    return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Number(date).toString(36);
}

export {
    getToken,
    unioformDateTransform,
    randomToken
}